﻿using DetectScars.Menus;
using DetectScars.model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DetectScars.common
{
    class CommonUtils
    {

        static string path = System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), "data/base/Probes.json");
        static string path_con = System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), "data/base/connects.json");
        static string path_piece = System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), "data/base/piece.json");
        static string path_PF = Path.Combine(System.IO.Directory.GetCurrentDirectory(), "data/base/pf.json");
        static string path_dir = System.IO.Path.Combine(Directory.GetCurrentDirectory(), "data/base");
        // 从config中读取appSettings中配置
        public static List<string> GetAppSetting(string key)
        {
            string? set = System.Configuration.ConfigurationManager.AppSettings[key];
            string res = "";
            if (set != null)
            {
                res = set.ToString();
            }
            //res;分割返回list
            List<string> list = new List<string>();
            if (set != null)
            {
                string[] arr = res.Split(';');
                foreach (string s in arr)
                {
                    list.Add(s);
                }
            }
            return list;
        }



        //初始化控件combobox
        public static void InitComboBox(System.Windows.Forms.ComboBox com, string str, Dictionary<string, string> dict)
        {
            //设置压缩方式
            List<string> list = CommonUtils.GetAppSetting(str);
            //com_CompressionType赋值
            com.Items.Clear();
            foreach (string s in list)
            {
                com.Items.Add(s);
            }
            //获取默认值
            //判断key是否存在
            if (dict.ContainsKey(str))
            {
                string value = dict[str];
                if (value != null)
                {
                    com.Text = value;
                }
                else
                {
                    //赋值选择框第一个
                    com.SelectedIndex = 0;
                }
            }
            else
            {
                //赋值选择框第一个
                com.SelectedIndex = -1;
            }
        }


        public static class AutoCloseMessageBoxHelper
        {
            public static void Show(string message, int duration = 1000)
            {
                using (var form = new AutoCloseMessageBox(message, duration))
                {
                    form.ShowDialog();
                }
            }
        }


        //从data/base下加载探头数据
        public static List<Probe> GetProbes()
        {

            List<Probe> probes = new List<Probe>();
            //不存在文件夹则创建
            if (!System.IO.Directory.Exists(path_dir))
            {
                System.IO.Directory.CreateDirectory(path_dir);
            }
            FileInfo fileInfo = new FileInfo(path);
            if (!fileInfo.Exists)
            {
                return probes;
            }
            string json = System.IO.File.ReadAllText(path);
            if (json != null)
            {
                probes = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Probe>>(json) ?? new List<Probe>();
            }
            return probes;
        }
        //写入探头数据到data/base下
        public static bool WriteProbes(List<Probe> probes)
        {
            try
            {
                string json = Newtonsoft.Json.JsonConvert.SerializeObject(probes);

                System.IO.File.WriteAllText(path, json);
            }
            catch (Exception e)
            {
                return false;
            }
            return true;

        }





        //从data/base下加载connect数据
        public static List<Connect> GetConnects()
        {
            List<Connect> cons = new List<Connect>();
            //不存在文件夹则创建
            if (!System.IO.Directory.Exists(path_dir))
            {
                System.IO.Directory.CreateDirectory(path_dir);
            }
            FileInfo fileInfo = new FileInfo(path_con);
            if (!fileInfo.Exists)
            {
                return cons;
            }
            string json = System.IO.File.ReadAllText(path_con);
            if (json != null)
            {
                cons = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Connect>>(json) ?? new List<Connect>();
            }
            return cons;
        }
        //写入Dac数据到data/base下
        public static bool WriteConnects(List<Connect> cons)
        {
            try
            {
                string json = Newtonsoft.Json.JsonConvert.SerializeObject(cons);
                System.IO.File.WriteAllText(path_con, json);
            }
            catch (Exception e)
            {
                return false;
            }
            return true;
        }

        //从data/base下加载零件数据
        public static List<Piece> GetPieces()
        {
            List<Piece> pieces = new List<Piece>();
            //不存在文件夹则创建
            if (!System.IO.Directory.Exists(path_dir))
            {
                System.IO.Directory.CreateDirectory(path_dir);
            }
            FileInfo fileInfo = new FileInfo(path_piece);
            if (!fileInfo.Exists)
            {
                return pieces;
            }
            string json = System.IO.File.ReadAllText(path_piece);
            if (json != null)
            {
                pieces = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Piece>>(json) ?? new List<Piece>();
            }
            return pieces;
        }
        //写入零件数据到data/base下
        public static bool WritePieces(List<Piece> pieces)
        {
            try
            {
                string json = Newtonsoft.Json.JsonConvert.SerializeObject(pieces);
                System.IO.File.WriteAllText(path_piece, json);
            }
            catch (Exception e)
            {
                return false;
            }
            return true;
        }

        //从data/base下加载配方数据
        public static List<PF> GetPFs()
        {
            List<PF> pfs = new List<PF>();
            //不存在文件夹则创建
            if (!System.IO.Directory.Exists(path_dir))
            {
                System.IO.Directory.CreateDirectory(path_dir);
            }
            FileInfo fileInfo = new FileInfo(path_PF);
            if (!fileInfo.Exists)
            {
                return pfs;
            }
            string json = System.IO.File.ReadAllText(path_PF);
            if (json != null)
            {
                pfs = Newtonsoft.Json.JsonConvert.DeserializeObject<List<PF>>(json) ?? new List<PF>();
            }
            return pfs;
        }
        //写入零件数据到data/base下
        public static bool WritePFs(List<PF> pfs)
        {
            try
            {
                string json = Newtonsoft.Json.JsonConvert.SerializeObject(pfs);
                System.IO.File.WriteAllText(path_PF, json);
            }
            catch (Exception e)
            {
                return false;
            }
            return true;
        }


    }
}
